package com.baihan.angus.app.game.busshow.adps;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.common.arouter_path.ArouterAppPath;
import com.baihan.angus.app.game.common.bean.response.ScriptRankResponse;

import java.util.List;

public class HotRankTopAdp extends RecyclerView.Adapter {

    private Context mContext;
    private List<ScriptRankResponse.Data> mScriptList;
    private RecyclerView mRecyclerView;

    public HotRankTopAdp(Context mContext, List<ScriptRankResponse.Data> mScriptList) {
        this.mContext = mContext;
        this.mScriptList = mScriptList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_hot_rank_script, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewHolder vh = (ViewHolder) holder;

        ScriptRankResponse.Data datas = mScriptList.get(position);
        if (datas.getTag().equals("newest")) {
            vh.ivTopType.setImageResource(R.mipmap.ic_new_pic);
        } else if (datas.getTag().equals("god")) {
            vh.ivTopType.setImageResource(R.mipmap.ic_god_pic);
        } else if (datas.getTag().equals("hot")) {
            vh.ivTopType.setImageResource(R.mipmap.ic_god_pic);
        }

        vh.llMore.setOnClickListener(view -> jumpToScriptRank());

        RecyclerView rvScript = vh.rvScipt;
        rvScript.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        RankScriptItemAdp mRankScriptItemAdp = new RankScriptItemAdp(mContext, datas.getList());
        rvScript.setAdapter(mRankScriptItemAdp);
        mRankScriptItemAdp.bindRecycleView(rvScript);
//        mRankScriptItemAdp.updateScriptList(datas.getList());
    }

    private void jumpToScriptRank() {
        ARouter.getInstance().build(ArouterAppPath.SCRIPT_RANK_ACTIVITY).navigation();
    }

    @Override
    public int getItemCount() {
        return mScriptList.size();
    }

    /**
     * 绑定recycleview
     *
     * @param recyclerView
     */
    public void bindRecycleView(RecyclerView recyclerView) {
        mRecyclerView = recyclerView;
        recyclerView.setAdapter(this);
    }

    /**
     * 更新信息
     */
    public void updateRankList(List<ScriptRankResponse.Data> scripts) {
        mScriptList.clear();
        mScriptList.addAll(scripts);
        if (mRecyclerView != null) {
            notifyDataSetChanged();
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView ivTopType;
        RecyclerView rvScipt;
        LinearLayout llMore;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivTopType = itemView.findViewById(R.id.ivTopType);
            rvScipt = itemView.findViewById(R.id.rvScipt);
            llMore = itemView.findViewById(R.id.llMore);
        }
    }
}
